package com.example.shoppingcart.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * 应用内私有存储-共享参数文件工具类
 */
public class SharedUtil {
    private static SharedUtil instance;
    private SharedPreferences preferences;

    private SharedUtil() {
    }

    public static SharedUtil getInstance(Context context) {

        if (instance == null) {
            instance = new SharedUtil();
            // 创建应用内私有共享参数文件
            instance.preferences = context.getSharedPreferences("shopping", Context.MODE_PRIVATE);
        }
        return instance;
    }

    public void writeBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public boolean readBoolean(String key, boolean defaultValue) {
        return preferences.getBoolean(key, defaultValue);
    }
}
